/* 
 * File:   fileIO.h
 * Author: Patrick W. M. Adams
 * Institution: Astrophysics, Cosmology and Gravity Centre,
 *              Department of Mathematics and Applied Mathematics,
 *              University of Cape Town
 *
 * Created on 13 March 2012, 1:22 AM
 */

#include "fileIO.h"

//default constructor
FileIO::FileIO(){
    
    fName = "";
    fLine = "";
    
}//end constructor

//argument constructor
FileIO::FileIO(std::string name){
    
    fName = name;
    fLine = "";
    
}//end constructor

//copy constructor
FileIO::FileIO(const FileIO& f){
    
    fName = f.fName;
    fLine = f.fLine;
    
}//end constructor

//destructor
FileIO::~FileIO(){
    
    //empty, but included for completeness
    
}//end destructor

//setFileName
void FileIO::setFileName(std::string name){
    
    fName = name;
    
}//end setFileName

//readFile
void FileIO::readFile(bool toConsole, std::vector<std::string>& v){
    
    std::vector<std::string> tempVec;
    
    std::ifstream inFile(fName.c_str()); //open a connection to the input file
    
    //check if the file was opened successfully; operate
    if(inFile.is_open()){
        
        //check which op must be done before operating
        if(toConsole){
            
            std::cout << "Printing to console\n" << std::endl;
        
            while(inFile.good()){
            
                    getline(inFile, fLine); //read file contents
            
                    std::cout << fLine << std::endl;
            }
            
        } else{                    
            
            std::cout << "Not printing to console\n" << std::endl;
            
            while(inFile.good()){                             
            
                getline(inFile, fLine); //read file contents
                        
                //check line contents
                if(fLine.length() == 0){
            
                    //std::cout << "QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ" << std::endl;
                    continue; //skip over this line; it's a comment
                    
                } else if((int)fLine.find("$#it simulation") != 0){
                    
                    std::cout << "Correct file format:\n\n" << std::endl;
                    std::cout << "$#it simulation" << std::endl;
                    std::cout << "$# eqn" << std::endl;
                    std::cout << "(Equations to be solved);" << std::endl;
                    std::cout << "$# grid" << std::endl;
                    std::cout << "(Grid parameters);" << std::endl;
                    std::cout << "$# steps" << std::endl;
                    std::cout << "(Simulation step data);" << std::endl;
                    std::cout << "$#it (Equations to be solved, named)" << std::endl;
                    std::cout << "Exiting..." << std::endl;
                    
                    break;
                        
                } else{
                    
                    while(inFile.good()){
                        
                        getline(inFile, fLine); //read the rest of the contents
                        
                        if((int)fLine.find("$# eqn") == 0 or
                           (int)fLine.find("$# grid") == 0 or
                           (int)fLine.find("$# steps") == 0){
                            
                            continue;
                            
                        } else if((int)fLine.find("$#it") == 0 or fLine.length() == 0){                           
                            
                            continue;
                        
                        } else{
                            
                            fLine = fLine.substr(0, fLine.find(";"));
                            boost::algorithm::trim(fLine); // trim again
                            boost::algorithm::split(tempVec, fLine, boost::algorithm::is_any_of(","));
                            
                            //copy elements over to v
                            for(unsigned int i = 0; i < tempVec.size(); i++){
                                
                                v.push_back(tempVec[i]);
                                
                            }
                            
                            continue;
                            
                        }
                        
                    }
                    
                    /* boost::algorithm::trim(fLine); //trim the line of any leading/trailing whitespace
                    //boost::algorithm::split(v, fLine, boost::algorithm::is_any_of("="));
                    fLine = fLine.substr(0, fLine.find(";"));
                    boost::algorithm::trim(fLine); // trim again
                    v.push_back(fLine);
                    //std::cout << fLine.substr(0, fLine.find("=")) << std::endl; */
                            
                }
            }                         
            
        }
        
    } else{
        
        std::cerr << "ERROR: Could not open file " << fName 
                  << " for reading." << std::endl;
        
    }
    
    inFile.close(); //disconnect and close input file
    
}//end readFile

//readParamFile
void FileIO::readParamFile(std::string name, std::vector<std::string>& v){
    
    std::ifstream inFile(name.c_str()); //open a connection to the input file
        
    
    //check if the file was opened successfully; operate
    if(inFile.is_open()){
        
        while(inFile.good()){
            
                getline(inFile, fLine); //read file contents
            
                v.push_back(fLine); //push param name onto vector
                
                //std::cout << "pushing " << fLine << std::endl;
                
        }            
        
    } else{
        
        std::cerr << "ERROR: Could not open file " << fName 
                  << " for reading." << std::endl;
        
    }
    
    inFile.close(); //disconnect and close input file
    
}//end readParamFile

//writeFile
void FileIO::writeFile(std::string data, std::string fileName){
    
    std::ofstream outFile(fileName.c_str()); //open a connection to the output file
    
    //check if the file was opened successfully; operate
    if(outFile.is_open()){
        
        outFile << data << std::endl;
        
    } else{
        
        std::cerr << "ERROR: Could not open file " << fName 
                  << " for writing." << std::endl;
        
    }
    
    outFile.close(); //disconnect and close output file
    
}//end writeFile

///operator=
FileIO& FileIO::operator=(const FileIO& f){
    
    //check for (invalid) self-assignment
    if(this != &f){
     
        //perform (deep) copy
        fName = f.fName;
        fLine = f.fLine;
        
    }
    
    return *this;
    
}//end operator=